package com.example.myapplication;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.receivers.CustomReceiver;
import com.example.myapplication.receivers.CustomReceiver2;
import com.example.myapplication.services.MyService;

public class AnimationActivity extends AppCompatActivity {

    private static final String TAG = "sun";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        final ImageView imageView = findViewById(R.id.frame);
        AnimationDrawable drawable = (AnimationDrawable) imageView.getDrawable();
        drawable.start();

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过加载xml动画设置文件，来创建一个Animation对象
                Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.alpha);
                imageView.startAnimation(animation);
            }
        });

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
            }
        });
        valueAnimator.start();

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView,
                "alpha", 0f, 1f);
        objectAnimator.setDuration(2000);
        objectAnimator.start();

        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });

    }

    public void btnClinkBlue(View view) {
        startActivity(new Intent(AnimationActivity.this, LayoutParamsActivity.class));
    }

    public void btnClinkPink(View view) {
        startActivity(new Intent(AnimationActivity.this, FragmentActivity.class));
    }

    public void btnStartService(View view) {
        Log.d(TAG, "btnStartService: ");
        startService(new Intent(this, MyService.class));
    }

    public void btnStopService(View view) {
        stopService(new Intent(this, MyService.class));
    }

    public void btnBindService(View view) {
        bindService(new Intent(this, MyService.class), connect, Context.BIND_AUTO_CREATE);
    }

    public void btnUnBindService(View view) {
        unbindService(connect);
    }

    //Activity与MyService之间的桥梁
    private ServiceConnection connect = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    //发送广播给注册的接受者
    public void btnClinkYellow(View view) {
        sendBroadcast(new Intent(this, CustomReceiver.class));
    }

    //动态注册接受者
    public void btnClinkGreen(View view) {
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.sun.project_");
        registerReceiver(new CustomReceiver2(), filter);
    }

    public void btnClinkRegister(View view) {
        Intent intent = new Intent();
        //必须和注册时保持一致
        intent.setAction("com.sun.project_");
        sendBroadcast(intent);
    }

    public void btnClinkMap(View view) {
        startActivity(new Intent(AnimationActivity.this, MapActivity.class));
    }
}